package com.example.wendao.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.PagedList;

import com.example.wendao.common.AsyncExecutor;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.News;
import com.example.wendao.model.entity.NewsPics;
import com.example.wendao.model.service.NewsPicsService;
import com.example.wendao.model.service.NewsService;

import java.util.List;

/**
 * 关于新闻模块的数据和操作，包括新闻、新闻图片
 */
public class NewsViewModel extends ViewModel {

    private NewsService newsService;

    private NewsPicsService newsPicsService;

    /**
     *  所有非热门新闻的分页列表
     */
    private LiveData<PagedList<News>> otherAllNews;

    /**
     *  最热的新闻列表
     */
    private LiveData<List<News>> hottestNewsList;


    /**
     *  全部新闻
     */
    private LiveData<PagedList<News>> allNews;

    {
        newsPicsService = new NewsPicsService();
        newsService = new NewsService();

        otherAllNews = new PageUtils<News>().buildLivePagedList(newsService.findOtherAllNews());
        hottestNewsList = newsService.findHeatNews();
        allNews = new PageUtils<News>().buildLivePagedList(newsService.findAllUser());
    }


    public LiveData<PagedList<News>> getAllNews(){
        return allNews;
    }

    public News[] findAllNewsByAuthorIdInArray(int id){
        return newsService.findAllNewsByAuthorIdInArray(id);
    }

    /**
     *
     * @return 所有非热门新闻的分页列表
     */
    public LiveData<PagedList<News>> getGeneralNewsList() {
        return otherAllNews;
    }

    /**
     *
     * @param news 要添加的新闻对象
     */
    public void addNews(News news) {
        new AsyncExecutor<>().execute(() -> newsService.insert(news));
    }

    /**
     *
     * @return 最火的新闻列表的LiveData形式
     */
    public LiveData<List<News>> getHottestNews() {
        return hottestNewsList;
    }

    /**
     *
     * @param title 新闻标题信息
     * @return 模糊查询得到的分页新闻列表
     */
    public LiveData<PagedList<News>> getNewsListByTitle(String title) {
        return new PageUtils<News>().buildLivePagedList(newsService.findNewsByTitle(title));
    }

    /**
     * 按照用户id搜索其名下新闻
     * @param newsauthorid  用户id
     * @return              新闻列表(数组)
     */
    public News[] getAllNewsByAuthorIdInArray(int newsauthorid){
        return new AsyncExecutor<News[]>().submit(() -> newsService.findAllNewsByAuthorIdInArray(newsauthorid));
    }

    /**
     *
     * @param newsId 要查询的新闻ID
     * @return 查询到的新闻
     */
    public News getNewsById(int newsId){
        return new AsyncExecutor<News>().submit(() -> newsService.findNewsById(newsId));
    }

    /**
     * 返回最新插入的新闻对象
     * @return  最新插入的新闻对象
     */
    public News findLatestNews(){
        return new AsyncExecutor<News>().submit(() -> newsService.findLatestNews());
    }

    /**
     *
     * @param news 要更新的新闻数组
     */
    public void updateSomeNews(News... news){
        new AsyncExecutor<>().execute(() -> newsService.updateNews(news));
    }


    /**
     *
     * @param news 要删除的新闻数组
     */
    public void deleteSomeNews(News... news){
        new AsyncExecutor<>().execute(() -> newsService.deleteNews(news));
    }

    /**
     * 根据传入的新闻ID序列查找到对应的论坛
     * @param newsIds   新闻ID序列 例如：（1,2,3,4,5）
     * @return          查询到的论坛分页列表
     */
    public LiveData<PagedList<News>> getNewsByNewsIds(List<Integer> newsIds){
        return new PageUtils<News>().buildLivePagedList(newsService.findSomeNewsByNewsIds(newsIds));
    }

    /**
     * @param newsPics 要添加的新闻图片列表
     */
    public void addNewsPic(NewsPics... newsPics){
        new AsyncExecutor<>().execute(() -> newsPicsService.insertNewsPic(newsPics));
    }

    /**
     *
     * @param newsId 要查询图片的新闻ID
     * @return 该新闻ID对应的图片列表LiveData形式
     */
    public LiveData<List<NewsPics>> getNewsPicsByNewsId(int newsId) {
        return newsPicsService.findNewsPicsByFatherId(newsId);
    }


    /**
     *
     * @param newsId 要查找封面的新闻ID
     * @return 查找到的新闻封面对象
     */
    public NewsPics getCoverByNewId(int newsId){
        return new AsyncExecutor<NewsPics>().submit(() -> newsPicsService.findCoverByFatherId(newsId));
    }

    /**
     *
     * @param newsPics 要更新的新闻图片
     */
    public void updateNewsPics(NewsPics... newsPics){
        new AsyncExecutor<>().execute(() -> newsPicsService.updateNewsPics(newsPics));
    }

    /**
     *
     * @param newsPics 要删除的新闻图片
     */
    public void deleteNewsPics(NewsPics... newsPics){
        new AsyncExecutor<>().execute(() -> newsPicsService.deleteNewsPics(newsPics));
    }

}

